// Component.cc

/*
   Copyright 2006 Corey Tabaka

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include <Component.h>
#include <Container.h>
#include <Dimension.h>
#include <Graphics.h>

namespace gui {

DEF_NULL_REF(Component);
	
Component::Component(void) : self(this, true), rect(0, 0, 0, 0), valid(false),
	visible(true), enabled(true), parent(0), forground(0x00, 0x00, 0x00, 0xff),
	background(0xff, 0xff, 0xff, 0xff) {
}

Component::Component(RefContainer c) : self(this, true), rect(0, 0, 0, 0),
	valid(false), visible(true), enabled(true), parent(c),
	forground(0x00, 0x00, 0x00, 0xff), background(0xff, 0xff, 0xff, 0xff) {
}

Component::~Component(void) {
}

RefComponent Component::getRef(void) {
	return self;
}

void Component::setForground(Color c) {
	forground = c;
}

void Component::setBackground(Color c) {
	background = c;
}

Color Component::getForground(void) {
	return forground;
}

Color Component::getBackground(void) {
	return background;
}

void Component::validate(void) {
	valid = true;
}

void Component::invalidate(void) {
	valid = false;
	
	if (parent != 0 && parent->valid) {
		parent->invalidate();
	}
}

void Component::repaint(RefGraphics g) {
	g->setColor(getBackground());
	g->fillRect(0, 0, rect.x2 - rect.x1, rect.y2 - rect.y1);
	g->setColor(getForground());
	
	paint(g);
}

void Component::paint(RefGraphics g) {
}

Dimension Component::getPreferredSize(void) {
	return getMinimumSize();
}

Dimension Component::getMinimumSize(void) {
	Dimension dim(rect.x2 - rect.x1, rect.y2 - rect.y1);
	return dim;
}

int Component::getWidth(void) {
	return rect.x2 - rect.x1;
}

int Component::getHeight(void) {
	return rect.y2 - rect.y1;
}

void Component::setLocation(int x, int y) {
	rect.x1 = x;
	rect.y1 = y;
}

void Component::setSize(int width, int height) {
	rect.x2 = rect.x1 + width;
	rect.y2 = rect.y1 + height;
	
	invalidate();
}

void Component::setBounds(int x, int y, int width, int height) {
	setLocation(x, y);
	setSize(width, height);
}

void Component::getBounds(Rectangle &r) {
	r.x1 = rect.x1;
	r.y1 = rect.y1;
	r.x2 = rect.x2;
	r.y2 = rect.y2;
}

void Component::setVisible(bool visible) {
	this->visible = visible;
}

bool Component::isVisible(void) {
	return visible;
}

bool Component::isRecursivelyVisible(void) {
	return visible && (parent == 0 || parent->isRecursivelyVisible());
}

RefContainer Component::getParent(void) {
	return parent;
}

void Component::setParent(RefContainer c) {
	parent = c;
}
	
} // namespace gui
